/**
 * 
 */
package ca.uwindsor.cs.deepweb.utility;

import java.io.File;

/**
 * @author Liang Jie
 * 
 */
public class FilesMover {

	/**
	 * The directory which contains the files to be checked
	 */
	protected File dataDir;

	protected int offset;

	protected long filesquality;
	
	protected long dirquality;

	protected String dir;

	public FilesMover(String dirpath) {
		dir = dirpath;
		dirquality = 0;
		filesquality = 0;
		offset = 1;
		dataDir = new File(dirpath);
	}

	public void move(long length) {
		
		File[] dataFiles = dataDir.listFiles();
		System.out.println("Start to moving the files!");
		String path = getNewDirectory();
		for (int i = 0; i < dataFiles.length; i++) {
			File file = dataFiles[i];
			if (file.isFile() && file.getName().endsWith(".txt")) {
				System.out.println(file.getName());
				filesquality++;
				if((filesquality)%length == 0){
					dirquality ++;
					path = getNewDirectory();
				} 
				boolean flag = file.renameTo( new File(path + file.getName()) );
				if(! flag ){
					System.err.println("Error when moving: "+file.getName());
				}
			}
		}
	}
	
	protected String getNewDirectory(){
		String newPath = dir + java.io.File.separator + dirquality;
		File fnewpath = new File(newPath);
		if (!fnewpath.exists())
			fnewpath.mkdirs();
		newPath += java.io.File.separator;
		return newPath;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		args = new String[2];
		args[0] = "SourceData//zh_wiki";
		args[1] = "10";
		FilesMover mover = new FilesMover(args[0]);
		mover.move(Integer.parseInt(args[1]));

	}

}
